﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

import Qt.labs.platform 1.1
import Qt.labs.settings 1.1
import QtQml 2.15
ColumnLayout{

    Settings{
           id:localSettings
           fileName: "conf/local.ini"
       }

    Rectangle{
        Layout.fillWidth: true
        width: parent.width
        height: 60
        color: "#00000000"
        Text{
            x:10
            verticalAlignment: Text.AlignBottom
            text: qsTr("本地音乐")
            font.family: window.mFONT_FAMILY
            font.pointSize: 25
            color:"white"
        }
    }
    RowLayout{
        height: 80
        Item{
            width: 5
        }
        MusicTextButton{
            btnText: "添加本地音乐"
            btnHeight: 50
            btnWidth: 200
            onClicked: fileDialog.open()
        }
        MusicTextButton{
                   btnText: "刷新记录"
                   btnHeight: 50
                   btnWidth: 120
                   onClicked: getLocal()
               }
               MusicTextButton{
                   btnText: "清空记录"
                   btnHeight: 50
                   btnWidth: 120
                   onClicked: saveLocal()
               }

    }
    MusicListView{
        id:localListView
        onDeleteItem: deleteLocal(index)

    }

    Component.onCompleted: {
        getLocal()
    }
    function getLocal(){
        var list = localSettings.value("local",[])
        localListView.musiclist = list
        return list
    }
    function saveLocal(list=[]){
        localSettings.setValue("local",list)
        getLocal()
    }
    function deleteLocal(index){
        var list = getLocal()
        if(list.length< index+1)return
        list.splice(index,1)
        saveLocal(list)
    }

    FileDialog{
        id:fileDialog
        fileMode: FileDialog.OpenFiles
        nameFilters: ["MP3 Music Files(*.mp3)","FLAC Music Files(*.flac)","OGG Music Files(*.ogg)"]
        folder: StandardPaths.standardLocations(StandardPaths.MusicLocation)[0]
        acceptLabel: "确定"
        rejectLabel: "取消"
        onAccepted: {
            var list = getLocal()
            for( var index in files)
            {
                var path =files[index]
                var arr = path.split("/")
                var fileNameArr = arr[arr.length - 1].split(".")
                fileNameArr.pop()
               // fileNameArr.join(".")
                var fileNmae = fileNameArr.join(".")
                var nameArr = fileNmae.split("-")
                var name = "歌曲名称"
                var artist = "作者"
                if(nameArr.length>1){
                    artist = nameArr[0]
                    nameArr.shift()
                }
                name = nameArr.join("-")
                if(list.filter(item=>item.id === path).length<1)
                                    list.push({
                                                  id:path+"",
                                                  name,artist,
                                                  url:path+"",
                                                  album:"本地音乐",
                                                  type:"1"//1表示本地音乐，0表示网络
                                              })

                                saveLocal(list)
                localListView.musiclist = list
            }
        }
    }
}


